<template>
  <div class="ele-body">
    <a-row :gutter="16">
      <!-- 左侧列表 -->
      <a-col :lg="4" :md="10" :sm="24" :xs="24" class="left-menu-height">
        <devicetype-tree
          :devicetypeList="devicetypeList"
          v-model:currentSelectDeviceId="where.dceId"
          @changeNodeSelect="reload"
        />
      </a-col>
      <!-- 右侧用户管理表格 -->
      <a-col :lg="20" :md="14" :sm="24" :xs="24">
        <!-- 搜索表单 -->
        <div class="block-interval">
          <a-card :bordered="false">
            <a-form layout="inline" :model="where">
              <a-row>

                <a-form-item label="设备编码:">
                  <a-input v-model:value.trim="where.recordNo" placeholder="请输入设备编码" allow-clear />
                </a-form-item>

                <a-form-item label="所属牧场:" name="orgId">
                  <a-select
                    show-search
                    v-model:value="where.orgId"
                    placeholder="请选择所属牧场"
                    style="width: 205px;"
                    :show-arrow="false"
                    :filter-option="false"
                    :not-found-content="null"
                    @change="selectRanch"
                  >
                    <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                      {{ item.ranchName }}
                    </a-select-option>
                  </a-select>
                </a-form-item>


                <a-form-item label="所属牛舍:" name="oxtId">
                  <a-select
                    show-search
                    v-model:value="where.oxtId"
                    placeholder="请选择所属牛舍"
                    style="width: 200px"
                    :show-arrow="false"
                    :filter-option="false"
                    :not-found-content="null"
                  >
                    <a-select-option v-for="item in oxtallList" :key="item.oxtallId" >
                      {{ item.oxtallName }}
                    </a-select-option>
                  </a-select>
                </a-form-item>

                <a-form-item class="ele-text-center">
                  <a-space>
                    <a-button type="primary" @click="reload">查询</a-button>
                    <a-button @click="reset">重置</a-button>
                  </a-space>
                </a-form-item>

              </a-row>
            </a-form>
          </a-card>
        </div>

        <!-- 表格 -->
        <div>
          <a-card :bordered="false" class="table-height">
            <ele-pro-table
              ref="table"
              row-key="recordId"
              :datasource="datasource"
              :columns="columns"
              :where="where"
              v-model:selection="selection"
              :scroll="{ x: 'max-content' }"
            >
              <!-- table上边工具栏 -->
              <template #toolbar>
                <a-space>
                  <a-button type="primary" @click="openEdit()">
                    <template #icon>
                      <plus-outlined />
                    </template>
                    <span>新建</span>
                  </a-button>
                </a-space>
              </template>

              <template #bodyCell="{ column, record }">
                <template v-if="column.key === 'statuscolumn'">
                  <a-tag v-if="record.recordStatus == 1" color="green">正常</a-tag>
                  <a-tag v-else color="red">异常</a-tag>
                </template>

                <!-- table操作栏按钮 -->
                <template v-if="column.key === 'action'">
                  <a-space>
                    <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                      <a class="ele-text-danger">删除</a>
                    </a-popconfirm>
                    <a-divider type="vertical" />
                    <a-dropdown>
                      <a class="ant-dropdown-link" @click.prevent>
                        更多
                        <DownOutlined />
                      </a>
                      <template #overlay>
                        <a-menu>
                          <a-menu-item >
                            <a @click="openEdit(record)">修改</a>
                          </a-menu-item>
                          <a-menu-item >
                            <a @click="openManager(record)">管理</a>
                          </a-menu-item>
                          <a-menu-item v-if="record.switchMappingDeviceRecordDTO != null">
                            <a @click="openPhaseManager(record.switchMappingDeviceRecordDTO)">电源管理</a>
                          </a-menu-item>
                        </a-menu>
                      </template>
                    </a-dropdown>
                  </a-space>
                </template>
              </template>
            </ele-pro-table>
          </a-card>
        </div>
      </a-col>
    </a-row>

    <!-- 编辑弹窗 -->
    <devicelist-edit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
    <component :is="currentView" v-if='showDeviceManager'  v-model:visible="showDeviceManager" :data="current" />

    <component :is="phaseView" v-if='showPhaseManager'  v-model:visible="showPhaseManager" :data="phaseCurrenData" />

    <!-- 设备管理 -->
    <!-- <device-spray-ctocabinet-modal v-if='showDeviceManager'  v-model:visible="showDeviceManager" :data="current" />-->
  </div>
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import { DeviceRecordApi } from '@/api/property/DeviceRecordApi';
import { DeviceApi } from '@/api/property/DeviceApi';
import DevicelistEdit from './devicelist-edit.vue';
import DevicetypeTree from './devicetype-tree.vue';
import DeviceSprayCtocabinetModal from './spray/ctocabinet-modal.vue'
import DeviceWatermeterModal from './watermeter/meter-modal.vue'
import DeviceSpraymianboardModal from './spraymainboard/mainboard-modal.vue'
import DeviceEnvControlModal from './envcontrol/envcontrol-modal.vue'
import DeviceLuoxungasModal from './luoxungas/luoxungas-modal.vue'
import DeviceLuoxunwaterpressureModal from './luoxunwaterpressure/luoxunwaterpressure-modal.vue'
import DeviceLuoxunwaterposModal from './luoxunwaterpos/luoxunwaterpos-modal.vue'
import DeviceLuoxunSimplephaseModal from './luoxunsimplephase/luoxunsimplephase-modal.vue'
import DeviceLuoxunThreephaseModal from './luoxunthreephase/luoxunthreephase-modal.vue'
import DeviceNoneManagerModal from './device-none-manager-modal.vue'
import DeviceLuoxunPulsationModal from './luoxunpulsation/luoxunpulsation-modal.vue'
import { RanchApi } from '@/api/property/RanchApi';
import { OxtallApi } from '@/api/property/OxtallApi';
import { message } from 'ant-design-vue';
import { DownOutlined } from '@ant-design/icons-vue';

export default {
  name: 'DeviceRecord',
  components: {
    PlusOutlined,
    DevicelistEdit,
    DevicetypeTree,
    DeviceSprayCtocabinetModal,
    DeviceWatermeterModal,
    DeviceSpraymianboardModal,
    DeviceEnvControlModal,
    DeviceLuoxungasModal,
    DeviceLuoxunSimplephaseModal,
    DeviceLuoxunThreephaseModal,
    DeviceLuoxunPulsationModal,
    DeviceNoneManagerModal,
    DeviceLuoxunwaterpressureModal,
    DeviceLuoxunwaterposModal,
    DownOutlined
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '牛舍',
          dataIndex: 'oxtallName'
        },
        {
          title: '区域',
          dataIndex: 'areaName'
        },
        {
          title: '设备类型',
          dataIndex: 'deviceName'
        },
        {
          title: '设备ID',
          dataIndex: 'recordId'
        },
        {
          title: '设备状态',
          key : 'statuscolumn',
          dataIndex: 'recordStatus',
        },
        {
          title: '当前版本',
          dataIndex: 'clientVersion'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          key: 'action',
          width: 140,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      devicetypeList : [],
      showDeviceManager : false,
      ranchList : [],
      oxtallList : [],
      currentView : '',
      //开关
      phaseView : '',
      showPhaseManager : false,
      phaseCurrenData : null
    };
  },
  async created() {
    let list = await DeviceApi.tree()
    this.devicetypeList = (list ? list.data : null) || []
    RanchApi.list({}).then(resp => {
      let _ranchList = (resp ? resp.data : null) || [];
      this.ranchList = _ranchList;
      if (_ranchList.length > 0) {
        OxtallApi.list({"orgId" : _ranchList[0].ranchId}).then(result => {
          this.oxtallList = (result ? result.data : null) || [];
        }).catch((err) => {
          console.log(err)
        });
      }
    });
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/03/05 14:43
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/03/05 14:43
     */
    reset() {
      this.where.recordNo = '';
      this.where.oxtId = '';
      this.where.orgId = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/03/05 14:43
     */
    async remove(row) {
      const result = await DeviceRecordApi.delete({ recordId: row.recordId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2022/03/05 14:43
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    openManager(row)
    {
      this.currentView = row.antdvComponent;
      this.current = row;
      this.showDeviceManager = true;
    },

    openPhaseManager(row)
    {
      this.phaseView = row.antdvComponent;
      this.phaseCurrenData = row;
      this.showPhaseManager = true;
    },

    selectRanch(ranchId) {
      this.oxtallList = [];
      this.where.oxtId = undefined;
      OxtallApi.list({"orgId" : ranchId}).then(result => {
        this.oxtallList = (result ? result.data : null) || [];
      }).catch((err) => {
        console.log(err)
      });
    },

    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return DeviceRecordApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
